﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Models;
using YouWei.Infrastructure.Service;
using YouWei.Op.Common;

namespace YouWei.Op.Controllers
{
    public class VehicleLicenseController : BaseController
    {
        VehicleLicenseService _service = new VehicleLicenseService();
        UserInfoService _userService = new UserInfoService();
        // GET
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public PartialViewResult ParList(VehicleLicenseSearchModel model)
        {

            var pageIndex = Convert.ToInt32(GetParam("page"));
            var pageSize = GetPageSize();
            var page = _service.Pages(pageIndex, pageSize, model);
            ViewBag.Total = page.total;
            ViewBag.PageIndex = pageIndex;
            ViewBag.PageSize = pageSize;
            return PartialView(page.rows);

        }
        public ActionResult Detail()
        {
            var userId = GetParam<int>("userId");
            var models = new List<VehicleLicense>();
            if (userId <= 0)
            {
                var vehicleId = GetParam<int>("id");
                var entity = _service.Single(vehicleId);
                if (entity != null)
                    models.Add(entity);
            }
            else
            {
                models = _service.GetByUser(userId);
            }

            return View(models);
        }
        /// <summary>
        /// 模态框 行驶证信息
        /// </summary>
        /// <returns></returns>
        public ActionResult ModalVehicleDetail()
        {
            var userId = GetParam<int>("userId");
            var models = new List<VehicleLicense>();
            if (userId <= 0)
            {
                var vehicleId = GetParam<int>("id");
                var entity = _service.Single(vehicleId);
                if (entity != null)
                    models.Add(entity);
            }
            else
            {
                models = _service.GetByUser(userId);
            }
            return View(models);
        }
        /// <summary>
        /// 更新行驶证状态和用户审核状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateUserVehicleStatus(VehicleLicense model)
        {
            ResultData<int> rt = ResultData<int>.Error("");

            if (model.VehicleLicenseID <= 0)
            {
                rt.message = "缺少必须的参数VehicleLicenseID";
                return ReturnResult(rt);
            }

            if (model.VehicleStatus <= 0)
            {
                rt.message = "缺少必要的参数VehicleStatus";
                return ReturnResult(rt);
            }
            var entity = _service.Single(model.VehicleLicenseID);
            if (entity != null)
            {
                entity.VehicleStatus = model.VehicleStatus;
                entity.UpdatedDate = DateTime.Now;
                //更新用户状态
                var user = _userService.Single(entity.UserID);
                if (user != null)
                {

                    //新审核车辆如果是通过，则直接把user表status改成通过
                    if (entity.VehicleStatus == 2)
                    {
                        user.VehicleStatus = entity.VehicleStatus;
                    }
                    //新审核车辆如果是驳回，则判断user表是否通过，如果user表status是通过，则不更新状态，否则更新成驳回。
                    if (entity.VehicleStatus == 3 && user.VehicleStatus != 2)
                    {
                        user.VehicleStatus = 3;
                    }
                    user.UpdateDate = DateTime.Now;
                    _userService.Update(user);
                }
                var re = _service.Update(entity);
                if (re)
                {
                    rt.message = "更新成功";
                    rt.status = 0;
                }
                else
                {
                    rt.message = "更新失败";
                }
            }
            else
            {
                rt.message = "未找到对应行驶证，检查VehicleLicenseID是否正确";
            }
            return ReturnResult(rt);
        }
    }
}